package com.bul.config;

import com.bul.config.SpringMvcConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

import javax.servlet.Filter;

public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};//加载spring环境的配置
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};//加载springmvc环境的配置
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //处理乱码
    //配置字符过滤器，处理中文
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter =new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}

//上是下的简化

//public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//    @Override
//    //加载springmvc环境的配置
//    protected WebApplicationContext createServletApplicationContext() {
//        //tomcat容器启动的时候就会加载这个类，然后就会加载spring的配置
//        AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
//        ctx.register(SpringMvcConfig.class);
//        return ctx;
//    }
//    //加载spring环境的配置
//    @Override
//    protected WebApplicationContext createRootApplicationContext() {
//        AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
//        ctx.register(SpringConfig.class);
//        return ctx;
//    }
//
//    @Override
//    protected String[] getServletMappings() {
//        //表示所有请求归springmvc处理
//        return new String[]{"/"};
//    }
//}
